{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import sys\n",
    "sys.path.append(r\"../src/data/\")\n",
    "# import \n",
    "import transform as Transforms\n",
    "from PIL import Image\n",
    "\n",
    "\n",
    "def mask_vis(label, img=None, alpha=0.5):\n",
    "    '''\n",
    "    :param label:原始标签 \n",
    "    :param img: 原始图像\n",
    "    :param alpha: 透明度\n",
    "    :return: 可视化标签\n",
    "    '''\n",
    "    r = np.where(label == 1, 255, 0)\n",
    "    g = np.where(label == 2, 255, 0)\n",
    "    b = np.where(label == 3, 255, 0)\n",
    "    yellow = np.where(label == 4, 255, 0)\n",
    "    anno_vis = np.dstack((b, g, r)).astype(np.uint8)\n",
    "    # 黄色分量(红255, 绿255, 蓝0)\n",
    "    anno_vis[:, :, 0] = anno_vis[:, :, 0] + yellow\n",
    "    anno_vis[:, :, 1] = anno_vis[:, :, 1] + yellow\n",
    "    anno_vis[:, :, 2] = anno_vis[:, :, 2] + yellow\n",
    "    if img is None:\n",
    "        return anno_vis\n",
    "    else:\n",
    "        overlapping = cv.addWeighted(img, alpha, anno_vis, 1-alpha, 0)\n",
    "        return overlapping\n",
    "\n",
    "\n",
    "def draw(image,mask):\n",
    "    plt.figure(figsize=(16,8))\n",
    "    plt.subplot(121)\n",
    "    plt.imshow(image)\n",
    "    plt.subplot(122)\n",
    "    plt.imshow(mask_vis(mask,image))\n",
    "    \n",
    "image = cv.imread(\"image_10_1500.png\")\n",
    "image = cv.cvtColor(image,cv.COLOR_BGR2RGB)\n",
    "mask = cv.imread(\"label_10_1500.png\",cv.IMREAD_GRAYSCALE)\n",
    "draw(image,mask)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = Transforms.opencv_transforms.RandomHorizontalFlip(p=1)\n",
    "sample = {'image':image,'mask':mask}\n",
    "t_sample = t(sample)\n",
    "draw(t_sample['image'],t_sample['mask'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = Transforms.opencv_transforms.RandomCrop(p=1,output_size=(256,256))\n",
    "sample = {'image':image,'mask':mask}\n",
    "t_sample = t(sample)\n",
    "draw(t_sample['image'],t_sample['mask'])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = Transforms.opencv_transforms.RandomVerticalFlip(p=1)\n",
    "sample = {'image':image,'mask':mask}\n",
    "t_sample = t(sample)\n",
    "draw(t_sample['image'],t_sample['mask'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = Transforms.opencv_transforms.ColorJitter(brightness=0.1,contrast=0.1,saturation=0.1,hue=0.1)\n",
    "sample = {'image':image,'mask':mask}\n",
    "t_sample = t(sample)\n",
    "draw(t_sample['image'],t_sample['mask'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
